//
//  HLDetailDiscussView.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/11.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLDetailDiscussView.h"
#import <Masonry/Masonry.h>
#import "HLDiscussCell.h"
#import "HttpRequestManager.h"
#import <MJExtension/MJExtension.h>
#import "MBProgressHUD+HL.h"
#import "HLCourseDiscuss.h"
#import <MJRefresh/MJRefresh.h>
#import "HLUserInfo.h"

#define kPageLimit 10

@interface HLDetailDiscussView()<UITableViewDelegate,UITableViewDataSource>
{
    NSUInteger _curren_page;
}
@property (nonatomic ,strong) UITableView *tableView;
/** 发起提问 */
@property (nonatomic ,strong) UIButton *questionButton;
/** 底部分割线  */
@property (nonatomic ,strong) UIView *line;
/** 底部View */
@property (nonatomic ,strong) UIView *bottomView;

/** 数据源  */
@property (nonatomic ,strong) NSMutableArray *dataSources;


@end

@implementation HLDetailDiscussView

#pragma mark - Lazy

- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        _dataSources = [NSMutableArray array];
    }
    return _dataSources;
}

- (UITableView *)tableView
{
    if (!_tableView ) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 150;
        _tableView.backgroundColor = HLRGB(245, 245, 245);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadRequestData)];
        _tableView.mj_footer.hidden = NO;
        [_tableView registerClass:[HLDiscussCell class] forCellReuseIdentifier:@"HLDiscussCell"];
    }
    return _tableView;
}

- (UIButton *)questionButton
{
    if (!_questionButton) {
        _questionButton = [[UIButton alloc]init];
        _questionButton.backgroundColor = kColorMineBlueBG;
        _questionButton.layer.cornerRadius = 15;
        _questionButton.clipsToBounds = YES;
        [_questionButton setTitle:@"发起提问" forState:UIControlStateNormal];
        [_questionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_questionButton addTarget:self action:@selector(askQuestionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _questionButton;
}


- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        [_bottomView addSubview:self.questionButton];
        [_bottomView addSubview:self.line];
    }
    return _bottomView;
}

- (UIView *)line
{
    if (!_line) {
        _line = [[UIView alloc]init];
        _line.backgroundColor = HLRGB(235, 235, 235);
    }
    return _line;
}

#pragma mark - UI

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
        [self.tableView.mj_footer beginRefreshing];
    }
    return self;
}

- (void)setUI
{
    _curren_page = 1;
    self.backgroundColor = HLRGB(245, 245,245);
    [self addSubview:self.tableView];
    [self addSubview:self.bottomView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.bottom.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(@(44));
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomView.mas_bottom);
        make.left.equalTo(self.bottomView);
        make.right.equalTo(self.bottomView);
        make.height.equalTo(@(1));
    }];
    
    [self.questionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.bottomView);
        make.height.equalTo(@(30));
        make.width.equalTo(@(self.width *0.5));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-44);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.top.equalTo(self);
    }];
}

#pragma mark - Request

- (void)loadRequestData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetQUesList signStr];
    params[@"course_id"] = self.courseID;
    params[@"user_id"] = [HLUserInfo account].userid;
    params[@"page"] = @(_curren_page);
    params[@"limit"] = @(kPageLimit);
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetQUesList parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        if ([responseObject[@"code"] integerValue] == 200) {
            NSArray *tempArray = [HLCourseDiscuss mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [weakSelf.dataSources addObjectsFromArray:tempArray];
            [weakSelf.tableView reloadData];
             _curren_page++;
            if (tempArray.count < kPageLimit) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
            }
        }else{
            [weakSelf.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
         [weakSelf.tableView.mj_footer endRefreshing];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}

- (void)reloadData
{
    /**** 用户发起提问 重新请求 删除数据 ****/
    [self.dataSources removeAllObjects];
    _curren_page = 1;
    self.tableView.mj_footer.hidden = NO;
    [self.tableView.mj_footer beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSources.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLCourseDiscuss *discuss = self.dataSources[indexPath.row];
    return discuss.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLDiscussCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HLDiscussCell"];
    cell.courseDiscuss = self.dataSources[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(detailDiscussView:questionListDidSelecte:)]) {
        HLCourseDiscuss *discuss = self.dataSources[indexPath.row];
        [_delegate detailDiscussView:self questionListDidSelecte:discuss];
    }
}

#pragma mark - Control

- (void)askQuestionButtonClick:(UIButton *)button
{
    if ([_delegate respondsToSelector:@selector(detailDiscussView:didClickAskQuestion:)]) {
        [_delegate detailDiscussView:self didClickAskQuestion:button];
    }
}

@end
